package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.plaf.*;
	import org.aswing.skinbuilder.SkinProgressBarBackground;
	import org.aswing.skinbuilder.SkinProgressBarForeground;
	
	public class PanelHPChange extends JPanel
	{
		public function PanelHPChange()
		{
			//component creation
			setSize(new IntDimension(400, 24));
			var layout0:EmptyLayout = new EmptyLayout();
			setLayout(layout0);
			this.setClipMasked(false);
		}
		private var numbers:Array = new Array;
		public function setHP(hp:int):void
		{
			if(hp == 0)
				return;
			var panel:JPanel = null;
			for each(panel in numbers)
			{
				if(this.contains(panel))
					this.remove(panel);
			}

			
			if(hp > 0)
			{
				panel = new JPanel();
				panel.setLocation(new IntPoint(0, 0));
				panel.setSize(new IntDimension(22, 24));
				panel.setPreferredSize(new IntDimension(22, 24));
				panel.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_number_green_add") as DisplayObject));
				panel.pack();
				append(panel);
				numbers.push(panel);
			}

			
			var str:String = hp.toString();
			for(var i:int; i < str.length; i++)
			{
				var number:String = str.substr(i,1);
				//if(number.length > 0)
				//{
					panel = new JPanel();
					panel.setLocation(new IntPoint(15*(i+1), 0));
					panel.setSize(new IntDimension(22, 24));
					panel.setPreferredSize(new IntDimension(22, 24));
					if(number == "-")
						panel.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_number_red_sub") as DisplayObject));
					else
						panel.setBackgroundDecorator(new AssetBackground(Client.getUIElement((hp > 0?"img_number_green_":"img_number_red_")+number) as DisplayObject));
					panel.pack();
					append(panel);
					numbers.push(panel);
				//}
			}
			setSize(new IntDimension(15*(numbers.length), 24));
			setPreferredSize(new IntDimension(15*(numbers.length), 24));
			this.pack();
			this.paintImmediately();
			
			if(!hasEventListener(Event.ENTER_FRAME)) 
				addEventListener(Event.ENTER_FRAME,onTimerUpdate);
		}
		
		public var _timeUpdate:Timer = null;
		public var _indexUp:int = -1;
		private var _tick:int = 0;
		public function onTimerUpdate(e:Event):void
		{
			_tick++;
			if(_tick > 1)
			{
				if(_indexUp != -1)
					(numbers[_indexUp] as JPanel).setLocationXY(15*_indexUp,0);
				if(_indexUp > 0)
					(numbers[_indexUp-1] as JPanel).setLocationXY(15*(_indexUp-1),0);
				if(_indexUp < numbers.length - 1)
					(numbers[_indexUp+1] as JPanel).setLocationXY(15*(_indexUp+1),0);
				_indexUp++;
				if(_indexUp < numbers.length)
				{
					(numbers[_indexUp] as JPanel).setLocationXY(15*_indexUp,-16);// = -5;
					if(_indexUp > 0)
						(numbers[_indexUp-1] as JPanel).setLocationXY(15*(_indexUp-1),-8);
					if(_indexUp < numbers.length - 1)
						(numbers[_indexUp+1] as JPanel).setLocationXY(15*(_indexUp+1),-8);
				}
				else
				{
					if(hasEventListener(Event.ENTER_FRAME)) 
						removeEventListener(Event.ENTER_FRAME,onTimerUpdate);
				}
				this.pack();
				this.paintImmediately();
				_tick = 0;
			}
		}
	}
}